'''
Created on Sep 22, 2012

@author: kingsfield
'''

import csv
from datetime import datetime

#train_file = '/home/kingsfield/Desktop/BestBuyPc/weak_predict/0'
train_file = '/home/kingsfield/Desktop/BestBuyPc/correct_train.csv'
sample_train_file = '/home/kingsfield/Desktop/BestBuyPc/weak_predict/0'
test_file = '/home/kingsfield/Desktop/BestBuyPc/correct_test.csv'
out_train_file = '/home/kingsfield/Desktop/BestBuyPc/fast_correct_train_small.csv'
out_test_file = '/home/kingsfield/Desktop/BestBuyPc/fast_correct_test.csv'


def readfile(f):
    infile = open(f)
    reader = csv.reader(infile, delimiter=",")
    reader.next() # burn the header
    return reader

def writefile(f):
    outfile = open(f, 'w')
    writer = csv.writer(outfile, delimiter=",")
    writer.writerow(["sku"])
    return writer

def get_date(click_time):
    try:
        t = datetime.strptime(click_time, '%Y-%m-%d %H:%M:%S.%f')
    except:
        t = datetime.strptime(click_time, '%Y-%m-%d %H:%M:%S')
    return t

st_date = datetime.strptime('2011-08-11 04:00:17', '%Y-%m-%d %H:%M:%S')
ed_date = datetime.strptime('2011-10-31 10:17:42', '%Y-%m-%d %H:%M:%S')


if __name__ == '__main__':
    'for train'
    user_dict = dict()
    reader = readfile(train_file)
    for (user, sku, category, raw_query, click_time, ___query_time) in reader:
        if user not in user_dict:
            user_dict[user] = len(user_dict)
            
    writer = open(out_train_file, 'w')
    writer.write('user,sku,category,query,click_time\n')
    reader = readfile(train_file)
    for (user, sku, category, raw_query, click_time, ___query_time) in reader:
        new_user = str(user_dict[user])
        new_click_time = str((get_date(click_time) - st_date).days)
        writer.write(",".join([new_user, sku, category, raw_query, new_click_time]) + '\n')
            
    'for test'
    reader = readfile(test_file)
    for (user, category, raw_query, click_time, ___query_time) in reader:
        if user not in user_dict:
            user_dict[user] = len(user_dict)
    writer = open(out_test_file, 'w')
    writer.write('user,sku,category,query,click_time\n')
    reader = readfile(test_file)
    for (user, category, raw_query, click_time, ___query_time) in reader:
        new_user = str(user_dict[user])
        new_click_time = str((get_date(click_time) - st_date).days)
        writer.write(",".join([new_user, category, raw_query, new_click_time]) + '\n')
        
    print 'done'
